﻿Imports System.Reflection

Public Class CompareForm

    Private _隐藏行头 As Boolean = False
    Public Property 隐藏行头() As Boolean
        Get
            Return _隐藏行头
        End Get
        Set(ByVal value As Boolean)
            _隐藏行头 = value
        End Set
    End Property

    Private Sub rad快照_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub CompareForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.chk隐藏行头.Checked = _隐藏行头

        RefreshLeft()

    End Sub

    Private Sub RefreshLeft()

        Dim dt As DataTable = IListenDb.CommonConUtil.GetTable("select Key_Timer as 快照时间,Descripion as 快照描述  from " + MyConfig.前缀 + "_Key order by Key_Timer desc ")

        dgLeft.DataSource = dt.Copy

        IListenDb.CommonConUtil.CloseCon()

    End Sub

    Private Sub dgLeft_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgLeft.SelectionChanged

        Dim sql As String
        Dim dt As DataTable

        If dgLeft.SelectedRows.Count < 1 Then
            dgTableLeft.DataSource = New DataTable
        Else
            sql = "select ID AS 编号,Key_Timer as 快照时间, U_TableName as 更新表名称,Table_UpdateTime as 更新表时间,TypeName as 更新类型 from " + MyConfig.前缀 + "_Table where Key_Timer=" & dgLeft.SelectedRows(0).Cells(0).Value
            dt = IListenDb.CommonConUtil.GetTable(sql)
            dgTableLeft.DataSource = dt
        End If

        If dgLeft.SelectedRows.Count < 2 Then
            dgTableRight.DataSource = New DataTable
        Else
            sql = "select ID AS 编号,Key_Timer as 快照时间, U_TableName as 更新表名称,Table_UpdateTime as 更新表时间,TypeName as 更新类型 from " + MyConfig.前缀 + "_Table where Key_Timer=" & dgLeft.SelectedRows(1).Cells(0).Value
            dt = IListenDb.CommonConUtil.GetTable(sql)
            dgTableRight.DataSource = dt
        End If

        IListenDb.CommonConUtil.CloseCon()

    End Sub

    Private Sub dgTableLeft_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgTableLeft.SelectionChanged

        If 刷新() = False Then

            Me.dgData.DataSource = Nothing

        End If

    End Sub

    Private Sub dgTableRight_SelectionChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgTableRight.SelectionChanged

        If 刷新() = False Then

            Me.dgData.DataSource = Nothing

        End If

    End Sub

    Private Function 刷新() As Boolean

        Dim selectedTable As New List(Of String)
        Dim selected编号 As New List(Of String)
        Dim sql As String

        For Each dgr As DataGridViewRow In dgTableLeft.SelectedRows
            If Not selectedTable.Contains(dgr.Cells("更新表名称").Value.ToString) AndAlso selectedTable.Count > 0 Then
                Return False
            End If
            selectedTable.Add(dgr.Cells("更新表名称").Value.ToString)
            selected编号.Add(dgr.Cells("编号").Value.ToString)
        Next

        For Each dgr As DataGridViewRow In dgTableRight.SelectedRows
            If Not selectedTable.Contains(dgr.Cells("更新表名称").Value.ToString) AndAlso selectedTable.Count > 0 Then
                selectedTable.Add(dgr.Cells("更新表名称").Value.ToString)
                Return False
            End If
            selectedTable.Add(dgr.Cells("更新表名称").Value.ToString)
            selected编号.Add(dgr.Cells("编号").Value.ToString)
        Next

        dgData.DataSource = Nothing

        If selected编号.Count > 0 Then

            Dim id As String = String.Join(",", selected编号.ToArray)

            sql = "select * from " & selectedTable(0) & " where " & MyConfig.前缀 & "_UpdateId in (" & id & ") order by " + MyConfig.前缀 + "_UpdateTimer"

            Dim dt As DataTable = IListenDb.CommonConUtil.GetTable(sql)
            dgData.DataSource = dt
            dgData.Tag = selectedTable(0)

            If chk隐藏行头.Checked = True Then
                For index As Integer = 0 To 3
                    dgData.Columns(index).Visible = False
                Next
            End If

            Dim updateTableName As String = selectedTable(0)
            updateTableName = updateTableName.Substring(MyConfig.前缀.Length + 1)

            sql = "SELECT name FROM sys.columns WHERE object_id=Object_Id('" & updateTableName & "') and column_id in (select colid from sysindexkeys where id=Object_Id('" & updateTableName & "') and indid=1 )"
            Dim dt主键 As DataTable = IListenDb.CommonConUtil.GetTable(sql)

            Dim lst主键索引 As New List(Of String)
            For index As Integer = 0 To dt主键.Rows.Count - 1
                For jdex As Integer = 0 To dgData.ColumnCount - 1
                    If dgData.Columns(jdex).Name = dt主键.Rows(index)("name") Then
                        dgData.Columns(jdex).HeaderText = dgData.Columns(jdex).HeaderText & "(" & "KEY)"
                        dgData.Columns(jdex).DisplayIndex = lst主键索引.Count
                        dgData.Columns(jdex).Frozen = True
                        lst主键索引.Add(dgData.Columns(jdex).Name)
                    End If
                Next
            Next

        End If

        Return True

    End Function

    Private Sub btn刷新_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn刷新.Click

        If 刷新() = False Then

            Me.dgData.DataSource = Nothing

        End If

    End Sub

    Private Sub btn比较_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn比较.Click

        Dim dt As DataTable = CType(dgData.DataSource, DataTable)
        If dt Is Nothing OrElse dt.Rows.Count = 0 Then
            Exit Sub
        End If

        Dim newTable As DataTable = dt.Clone

        For index As Integer = 0 To dgData.Rows.Count - 1
            newTable.ImportRow(dt.Rows(index))
        Next

        Dim updateTableName As String = dgData.Tag.ToString
        updateTableName = updateTableName.Substring(MyConfig.前缀.Length + 1)

        Dim sql As String = "SELECT name FROM sys.columns WHERE object_id=Object_Id('" & updateTableName & "') and column_id in (select colid from sysindexkeys where id=Object_Id('" & updateTableName & "') and indid=1 )"
        Dim dt主键 As DataTable = IListenDb.CommonConUtil.GetTable(sql)

        Dim frm As IListenDb.ListDataBase = 得到浏览数据窗体()
        If frm Is Nothing Then
            wys.util.Common.MessageBase.ShowMessage("没有数据浏览窗体")
            Exit Sub
        End If
        frm.隐藏行头 = chk隐藏行头.Checked
        frm.数据 = newTable
        frm.主键 = dt主键
        frm.前缀 = MyConfig.前缀
        frm.Show()

    End Sub

    Private Function 得到浏览数据窗体() As IListenDb.ListDataBase

        Dim 所有组件 As List(Of 组件表) = 组件表操作类.GetAll组件

        Dim lstDataNew As IListenDb.ListDataBase
        If 所有组件.Count > 0 Then
            For index As Integer = 所有组件.Count - 1 To 0 Step -1
                If 所有组件(index).浏览数据窗体 = 1 Then
                    lstDataNew = Assembly.Load(所有组件(index).组件名称).CreateInstance(所有组件(index).组件名称 & ".ListData")
                    If lstDataNew IsNot Nothing Then
                        Return lstDataNew
                    End If
                End If
            Next
        End If

        Return Nothing

    End Function

End Class
